package com.shntec.temp.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shntec.bp.json.JSONArray;
import com.shntec.bp.json.JSONException;
import com.shntec.bp.json.JSONObject;

import com.shntec.bp.common.ShntecBaseAction;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.common.JSONMessageConstant;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.util.DatabaseManager;
import com.shntec.bp.util.ShntecLogger;
import com.shntec.temp.util.MySQLConn;

public class SetMeetingAction extends ShntecBaseAction {

	public SetMeetingAction () {
		super.actionCode = 20003;
		super.actionName = "SetMeetingAction";
		super.actionDescription = "设置会议记录信息";
		// No need authentication
		super.needAuthenticationCheck = false;
	}

	@Override
	public ResponseMessageGenerator handleAction(RequestMessageParser parser) {

		ResponseMessageGenerator generator = new ResponseMessageGenerator();

		JSONObject payload = null;
		JSONObject parameter = parser.getParameterJsonObject();
		
		
		if (parameter == null) {

			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER);
		}
		
		int mid = -1;
		String today = null;
		String com = null;
		String question = null;
		
		
		try {
			mid = parameter.getInt("mid");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"mid\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"mid\"");
		}
		
		try {
			today = parameter.getString("today");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"today\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"today\"");
		}
		
		try {
			com = parameter.getString("com");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"com\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"com\"");
		}
		
		try {
			question = parameter.getString("question");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"question\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"question\"");
		}
		
		MySQLConn mysql = new MySQLConn();
		Connection conn = null;
		payload = new JSONObject();
		PreparedStatement pstmt = null;
		
		try {
			conn = mysql.getConn();
			pstmt = conn.prepareStatement("update temp_meeting set todayplans = ?,completion = ?,question = ? where mid = ?");
			pstmt.setString(1, today);
			pstmt.setString(2, com);
			pstmt.setString(3, question);
			pstmt.setInt(4, mid);
			
			int result = pstmt.executeUpdate();
			
			if(result == 1){
				if(today == null || today.equals("")){
					payload.put("today", false);
				}else{
					payload.put("today", true);
				}
				if(com == null || com.equals("")){
					payload.put("com", false);
				}else{
					payload.put("com", true);
				}
				if(question == null || question.equals("")){
					payload.put("question", false);
				}else{
					payload.put("question", true);
				}
				return generator.toSuccess(parser, payload);
			}else{
				return generator.toError(parser, 20020, "保存时出错！");
			}
			
			
		} catch (ClassNotFoundException e) {
			ShntecLogger.logger.error("ClassNotFoundException" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"ClassNotFoundException");
		} catch (SQLException e) {
			ShntecLogger.logger.error("SQLException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"SQLException");
		} catch (JSONException e) {
			ShntecLogger.logger.error("JSONException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"JSONException");
		}finally{
			if(conn != null){
				
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			
		}
		
	}

}
